# 内置模块 collections base64 hashlib contextlib urllib xml.parsers/xml.etree html.parser
import collections
print(collections.OrderedDict, collections.defaultdict, collections.ChainMap, collections.Counter)

import base64
s = '苹果'.encode()
print(base64.b16encode(s))
print(base64.urlsafe_b64encode(s))  # urlsafe将b64后的+和/变成-和_


import struct  # 将任意数据类型转换为字节码
b_data = struct.pack('>I', 1234)  # >I 中>为大端表示法，也是网络字节序，I是4字节无符号整数，H是2字节无符号整数
print(b_data)
print(struct.unpack('>I', b_data))  # 字节转化为整数

import hashlib
md5 = hashlib.md5()
md5.update('fuck'.encode())
md5.update('what'.encode())
print(md5.hexdigest())

sha1 = hashlib.sha1()
sha1.update('fuck'.encode())
sha1.update('what'.encode())
print(sha1.hexdigest())

import hmac  # 类似于md5加盐计算一个串，相比md5加盐更安全高效

h = hmac.new(b'secret', b'hello', digestmod='md5')
print(h.hexdigest())

import itertools
print(itertools.repeat('A')) # 无限迭代器
print(itertools.count(1)) # 无线迭代器
for k, g in itertools.groupby('abcabcefg'):
    print(k, list(g))


# 能使用with语句的对象都是实现了__enter__和__exit__这两个方法，但是每个都实现很麻烦，所以python提供了@contextmanager
from contextlib import contextmanager
class Query(object):

    def __init__(self, name):
        self.name = name

    def query(self):
        print('Query info about %s...' % self.name)

@contextmanager  # 让任意对象支持with语句
def create_query(name):
    print('Begin')
    q = Query(name)
    yield q   # 需使用yield返回
    print('End')

with create_query('tom') as q:
    q.query()

from contextlib import closing  # 如果一个对象没有标注contextmanager, closing可以对任意对象生成上下文
from urllib.request import urlopen
with closing(urlopen('http://www.baidu.com')) as page:
    c = 0
    for p in page:
        c += 1
    print(f'lines : {c}')



from xml.parsers.expat import ParserCreate
class DefaultSaxHandler(object):
    def start_element(self, name, attrs):
        print('sax:start_element: %s, attrs: %s' % (name, str(attrs)))

    def end_element(self, name):
        print('sax:end_element: %s' % name)

    def char_data(self, text):
        print('sax:char_data: %s' % text)

xml = r'''<?xml version="1.0"?>
<ol>
    <li><a href="/python">Python</a></li>
    <li><a href="/ruby">Ruby</a></li>
</ol>
'''

handler = DefaultSaxHandler()
parser = ParserCreate()
parser.StartElementHandler = handler.start_element
parser.EndElementHandler = handler.end_element
parser.CharacterDataHandler = handler.char_data
parser.Parse(xml)


# 外部xml处理库
import xml.etree.cElementTree as et
xml_text = """<ol>
    <li><a href="/python">Python</a></li>
    <li><a href="/ruby">Ruby</a></li>
</ol>"""
xml_tree = et.fromstring(xml_text)
print(xml_tree.find('li').find("a").text)
print(xml_tree.find('li/a').text)
print(xml_tree.findtext("li/a"))
print(xml_tree.findall("li/a"))

import xml.dom.minidom
xml_ele = xml.dom.minidom.parseString(xml_text)
ele_arr = xml_ele.getElementsByTagName("a")
print(ele_arr)



# html 解析
from html.parser import HTMLParser
from html.entities import name2codepoint

class MyHTMLParser(HTMLParser):

    def handle_starttag(self, tag, attrs):
        print('<%s>' % tag)

    def handle_endtag(self, tag):
        print('</%s>' % tag)

    def handle_startendtag(self, tag, attrs):
        print('<%s/>' % tag)

    def handle_data(self, data):
        print(data)

    def handle_comment(self, data):
        print('<!--', data, '-->')

    def handle_entityref(self, name):
        print('&%s;' % name)

    def handle_charref(self, name):
        print('&#%s;' % name)

parser = MyHTMLParser()
parser.feed('''<html>
<head></head>
<body>
<!-- test html parser -->
    <p>Some <a href=\"#\">html</a> HTML&nbsp;tutorial...<br>END</p>
</body></html>''')  # feed()方法可以多次调用，也就是不一定一次把整个HTML字符串都塞进去，可以一部分一部分塞进去。

